home *** CD-ROM | disk | FTP | other *** search
/ The Original Shareware 1.1 / The Original Shareware (WeMake CDs)(Volume 1.1)(CDs, Inc)(1993).iso / 16 / fpc225_3.zip / F-PCHELP.ZIP / ASSEM.TXT next >
Text File  |  1988-09-23  |  20KB  |  393 lines

  1. VIII.   PASM, THE F-PC ASSEMBLER
  2.  
  3.           
  4.           PASM.SEQ is an assembler which is based on an 8086 assembler
  5.           published in Dr. Dobb's Journal, February 1982, by Ray Duncan.
  6.           This assembler was subsequently modified by Robert L. Smith to
  7.           repair bugs, and support the prefix assembler notation.  Bob
  8.           discovered a very simple method to let a postfix assembler to
  9.           assemble prefix code, by deferring assembly until the next
  10.           assembler command or the end of line, when all the arguments for
  11.           the previous assembler command are piled on the top of the data
  12.           stack.  Tom Zimmer has made additional modifications to allow
  13.           syntax switching, and to increase compatibility in postfix mode
  14.           with the F83 Assembler.
  15.           
  16.           Writing assembly programs is black magic.  It is not appropriate
  17.           to discuss the joy and frustrations in working at such a low
  18.           level in this manual.  However, F-PC provides the best
  19.           environment for you to do experiments using assembly language,
  20.           because you can first verify the algorithm and methodology in
  21.           high level Forth code and gradually reducing the code to the
  22.           assembly level.  You will find numerous examples in which the
  23.           high level code in F83 is recoded in assembly, in addition to
  24.           many of the F83 kernel words which were in assembly already.
  25.           
  26.           The best way to learn 8086 assembly language is to use PASM,
  27.           armed with all the code words in F-PC as templates and examples.
  28.           Factor your high level words carefully so that words at the
  29.           bottom level can be conveniently recoded in assembly.  Take the
  30.           kernel words as templates to start with, and modify them so that
  31.           they will do exactly what you want them to do.
  32.           
  33.  
  34. 1.  PREFIX OR POSTFIX ?
  35.  
  36.           
  37.           PASM supports dual syntaxes.  The words PREFIX and POSTFIX switch
  38.           between the two supported modes.  The postfix mode is very
  39.           similar to F83's CPU8086 Assembler.  Prefix mode, which is the
  40.           default mode, allows a syntax which is much closer to MASM used
  41.           by Intel and MicroSoft.
  42.           
  43.           The assembler supports prefix syntax in an attempt to provide a
  44.           syntax which is more readable to programmers of other languages.
  45.           The use of sequential text file for source code encourages the
  46.           programmer to write programs in the vertical code style with one
  47.           statement per line.  This style is what traditional assembler
  48.           requires.  F-PC works well in this style, if you choose to do so.
  49.           However, F-PC does not prevent you to write in the horizontal
  50.           code style, by which you can squeeze many statements into one
  51.  
  52.           line and make you own life miserable.   It supports postfix
  53.           syntax to prevent alienating the established base of F83 users.
  54.           
  55.           The prefix notation is close to the original Intel assembly
  56.           syntax, and certainly will be more familiar to programmers of
  57.           other languages.  All the code words defined in F-PC are coded in
  58.           the prefix notation.  Please consider writing any new assembly
  59.           code you need in the prefix mode for distribution and
  60.           assimilation.
  61.           
  62.           The assembly of a machine instruction is generally deferred to
  63.           the following three events: when the next assembly mnemonic is
  64.           encountered, at the end of a line, or when the command END-CODE
  65.           or A; is executed.  Therefore, a good style in writing code words
  66.           in F-PC is to put one assembly instruction in one line, followed
  67.           by the parameter specification or the arguments.  Multiple
  68.           assembly instructions are allowed in the same line, except the
  69.           assembly directives which build control structures in a code
  70.           word, such as IF, ELSE, THEN, BEGIN, WHILE, AGAIN, etc.  These
  71.           directives must be the first or the only instruction in a line
  72.           because they act immediately, not waiting for the next assembly
  73.           instruction.  It is a good ideal to put these structure words in
  74.           separate lines with proper indentation so that the nested
  75.           structures in a code definition can be perceived more readily.
  76.  
  77.           
  78. 2.   PASM GLOSSARY
  79.  
  80.           
  81.           Here we will only give a small list of of PASM words in this
  82.           glossary.  All assembly mnemonics are identical to those defined
  83.           in F83 8086 Assembler.  All the structure directives and test
  84.           conditions are also identical to those in F83.  Only the most
  85.           important FORTH words controlling the assembler are listed here.
  86.           
  87.           PREFIX    Assert prefix mode for the following code definitions.
  88.           
  89.           POSTFIX   Assert postfix mode for the following code definitions.
  90.           
  91.           CODE      Define "name" as a new code definition. Assembly
  92.           language follows, terminated by END-CODE.
  93.           
  94.           END-CODE  Terminates CODE definitions, checks error conditions,
  95.           and makes the code definition available for searching and
  96.           execution.
  97.           
  98.           A;        Completes the assembly of the previous instruction.
  99.           
  100.           BYTE      Assemble current and subsequent code using byte
  101.           arguments, if register size is not explicitly specified.
  102.  
  103.           WORD      Assemble current and subsequent code using 16 bit
  104.           arguments, if register size is not explicitly specified.
  105.           
  106.           LABEL     Start an assembly subroutine or mark the current code
  107.           address to be referenced later.
  108.           
  109.           
  110. 3.   SYNTAX COMPARISON
  111.  
  112.           
  113.           The differences among the F-PC prefix mode, the F83 postfix mode,
  114.           and the Intel MASM notation are best illustrated by the following
  115.           table.  Although the table is not exhaustive, it covers most of
  116.           the cases useful in doing PASM programming.  You are welcome to
  117.           suggest additional cases to be included in this table.
  118.           
  119.           
  120.           
  121.           PREFIX                  POSTFIX                 MASM
  122.           
  123.           AAA                     AAA                     AAA
  124.           ADC AX, SI              SI AX ADC               ADC AX,SI
  125.           ADC DX, 0 [SI]          0 [SI] DX ADC           ADC DX,0[SI]
  126.           ADC 2 [BX+SI], DI       DI 2 [BX+SI] ADC        ADC 2[BX][SI],DI
  127.           ADC MEM BX              BX MEM #) ADC           ADC MEM,BX
  128.           ADC AL, # 5             5 # AL ADC              ADC AL,5
  129.           AND AX, BX              BX AX AND               AND AX,BX
  130.           AND CX, MEM             CX MEM #) AND           AND CX,MEM
  131.           AND DL, # 3             3 # DL AND              AND DL,3
  132.           CALL NAME               NAME #) CALL            CALL NAME
  133.           CALL FAR [] NAME        FAR [] NAME #) CALL     ?????
  134.           CMP DX, BX              BX DX CMP               CMP DX,BX
  135.           CMP 2 [BP], SI          SI 2 [BP] CMP           CMP [BP+2],SI
  136.           DEC BP                  BP DEC                  DEC BP
  137.           DEC MEM                 MEM DEC                 DEC MEM
  138.           DEC 3 [SI]              3 [SI] DEC              DEC 3[SI]
  139.           DIV CL                  CL DIV                  DIV CL
  140.           DIV MEM                 MEM DIV                 DIV MEM
  141.           IN PORT# WORD           WORD PORT# IN           IN AX,PORT#
  142.           IN PORT#                PORT# IN                IN AL,PORT#
  143.           IN AX, DX               DX AX IN                IN AX,DX
  144.           INC MEM                 BYTE MEM INC            INC MEM BYTE
  145.           INC MEM WORD            MEM #) INC              INC MEM WORD
  146.           INT 16                  16 INT                  INT 16
  147.           JA NAME                 NAME JA                 JA NAME
  148.           JNBE NAME               NAME #) JNBE            JNBE NAME
  149.           JMP NAME                NAME #) JMP             JMP
  150.           JMP FAR [] NAME         NAME [] FAR JMP         JMP [NAME]
  151.           JMP FAR $F000 $E987                             JMP F000:E987
  152.           LODSW                   AX LODS                 LODS WORD
  153.           LODSB                   AL LODS                 LODS BYTE
  154.           LOOP NAME               NAME #) LOOP            LOOP NAME
  155.           MOV DX, NAME            NAME #) DX MOV          MOV DX,[NAME]
  156.           MOV AX, BX              BX AX MOV               MOV AX,BX
  157.           MOV AH, AL              AL AH MOV               MOV AH,AL
  158.           MOV BP, 0 [BX]          0 [BX] BP MOV           MOV BP,0[BX]
  159.           MOV ES: BP, SI          ES: BP SI MOV           MOV ES:BP,SI
  160.           MOVSW                   AX MOVS                 MOVS WORD
  161.           POP DX                  DX POP                  POP DX
  162.           POPF                    POPF                    POPF
  163.           PUSH SI                 SI PUSH                 PUSH SI
  164.           REP                     REP                     REP
  165.           RET                     RET                     RET
  166.           ROL AX, # 1             AX ROL                  ROL AX,1
  167.           ROL AX, CL              AX CL ROL               ROL AX,CL
  168.           SHL AX, # 1             AX SHL                  SHL AX,1
  169.           XCHG AX, BP             BP AX XCHG              XCHG AX,BP
  170.           XOR CX, DX              DX, CX XOR              XOR CX,DX
  171.           
  172.           
  173. 4.   ADDRESSING MODES
  174.           
  175.           
  176.           The most difficult problem in using 8086 assembler is to figure
  177.           out the correct addressing mode and code it into an instruction.
  178.           You can get a good ideal and probably figure out most of the
  179.           addressing mode syntax from the above table.  However, there are
  180.           cases the table fells short.  Here we will try to summarize the
  181.           addressing syntax more systematically to show you how F-PC
  182.           handles addresses in the prefix mode.
  183.           
  184.         Register Mode
  185.           
  186.           Source or destination is a register in the CPU.  The source
  187.           registers are:
  188.           
  189.                AL  BL  CL  DL  AH  BH  CH  DH
  190.                AX  BX  CX  DX  SPECIFICATIONS  BP  SI  DI  IP  RP  CS  DS
  191.           SS  ES
  192.           
  193.           Destination register specifications are:
  194.           
  195.                AL, BL, CL, DL, AH, BH, CH, DH,
  196.                AX, BX, CX, DX, SPECIFICATIONS, BP, SI, DI, IP, RP, CS, DS,
  197.           SS, ES,
  198.           
  199.  
  200.         Immediate Mode
  201.           
  202.           The argument is assembled as a literal in the instruction.  The
  203.           immediate value must be preceded by the symbol #, which is a word
  204.           and must be delimited by spaces:
  205.           
  206.                     MOV  AX, # 1234
  207.                     ADD  CL, # 32
  208.                     ROL  AX, # 3
  209.           
  210.           
  211.         Direct Mode
  212.           
  213.           An address is assembled into the instruction.  This is used to
  214.           specify an address to be jumped to or a memory location for data
  215.           reference.  The address is used directly as a 16 bit number.
  216.           Depending on the instruction, the address may be assembled
  217.           unmodified or assembled as an eight bit offset in the branch
  218.           instructions.  To jump or call beyond a 64K byte segment, the
  219.           address must be preceded by FAR [] .  Examples are:
  220.           
  221.                     CALL FAR [] <label>
  222.                     JMP  <dest>
  223.                     MOV  BX, <source>
  224.                     INC  <dest> WORD
  225.                     JZ   <label>
  226.           
  227.           The destination address may be taken from the data stack
  228.           directly:
  229.           
  230.                     MOV  CX, # 16
  231.                     HERE ( save current code address on stack)
  232.                          ...
  233.                          ...
  234.                     LOOPZ ( loop back to HERE if condition fails)
  235.           
  236.           
  237.         Index Mode
  238.           
  239.           One or two registers can be used as index registers to scan
  240.           through data arrays.  The contents of the index register or the
  241.           sum of the contents of two index registers are added to form a
  242.           base address, an offset is added to the base address to form the
  243.           true address for data reference.  Examples are:
  244.           
  245.                     CMP  2 [BP], SI
  246.                     DEC  3 [SI]
  247.                     MOV  BP, 0 [BX]
  248.           
  249.           The following register index specifications are allowed in F-PC:
  250.           
  251.                [SI]  [IP]  [BP]  [RP]  [DI]  [BX]
  252.                [BX+SI]  [SI+BX]  [BX+IP]  [IP+BX]  [BX+DI]  [DI+BX]
  253.                [BP+SI]  [SI+BP]  [BP+IP]  [IP+BP]  [RP+IP]  [IP+RP]
  254.                [BP+DI]  [DI+BP]  [RP+DI]  [DI+BP]  [RP+DI]  [DI+RP]
  255.           
  256.           There must be an offset number preceding the index register
  257.           specification, even if the offset is 0.  When the index register
  258.           is used as destination, a comma must be appended immediately:
  259.           
  260.                     MOV  0 [BX+IP], AX
  261.  
  262.         Implied Mode and Segment Override
  263.           
  264.           The implied mode is where mistakes are most likely to occur
  265.           because you will have to be keenly aware of which segment
  266.           register is used by the instruction at any instance.  Since the
  267.           segment register is implied and not stated explicitly, the bug
  268.           generally can hide very securely underneath laughing at you.  The
  269.           code works when you test it but fails when the segment register
  270.           is modified.
  271.           
  272.           Branch and jump instructions use CS segment register.
  273.           
  274.           Data movement instructions use DS segment register.
  275.           
  276.           Stack instructions use SS segment.
  277.           
  278.           String instructions use DS:SI as source and ES:DI as destination.
  279.           
  280.           If you need to specify an address with a segment register other
  281.           than the default implied register, use a segment override
  282.           instruction before the address specification:
  283.           
  284.                     CS:   DS:  ES:  SS:
  285.           
  286.           Examples are:
  287.           
  288.                     MOV  ES: BP, SI
  289.                     CMP  CS: 2 [BP], AX
  290.                     ADD  AX, ES: 10 [BX+DI]
  291.           
  292.           The 8086 addressing modes are so confusing that even experienced
  293.           programmer needs a good Intel 8086 manual to find the right
  294.           addressing mode and the F-PC assembler syntax table to determine
  295.           the correct argument list.
  296.           
  297.           The best way to write assembly code is still keeping the code
  298.           short and simple.  It is very easy in F-PC to break a long CODE
  299.           definition into many small fragments which are initially defined
  300.           as separate CODE definitions.  After verifying that each fragment
  301.           works, you can edit out the CODE, NEXT, and END-CODE lines to
  302.           combine the fragments into a single CODE definition.
  303.           
  304.           Charles Curley kindly contributes an 8086 disassembler with a
  305.           single step debugger.  It is helpful to disassemble the CODE word
  306.           you defined and see what the computer thinks of what you mean.
  307.           There is always this 'Do what I mean, not what I say' syndrome.
  308.           Stepping through a piece of code one instruction at a time is the
  309.           last thing you have to do if everything else failed.
  310.                
  311.           
  312. 5.   MACROS IN PASM
  313.  
  314.           
  315.           Another area of interest is the macros, here is the definition of
  316.           the NEXT macro:
  317.           
  318.                     : NEXT  >PRE    JMP >NEXT A;    PRE> ;
  319.           
  320.           The macro itself is simply the sequence JMP >NEXT. The
  321.           surrounding words are used for support. Since PASM supports both
  322.           postfix as well as prefix notation, it is not known on entry to a
  323.           macro what mode is selected. The words >PRE and PRE> select
  324.           prefix, and restore the previous mode so macros will always be in
  325.           prefix notation. The A; after >NEXT, forces the assembly of the
  326.           JMP instruction before the mode switch.
  327.           
  328.           You can find many other examples of assembly macros in PASM.SEQ,
  329.           like 1PUSH, 2PUSH, and all the structure building directives.
  330.           
  331.           
  332. 6.   LOCAL LABEL
  333.  
  334.           
  335.           To support large code definitions, Bob Smith introduced 'local
  336.           labels' to F-PC.  The local lables are place markers $: preceded
  337.           by a number.  They are used to mark locations in a large code
  338.           definition for forward and backward jumps and branches.  They can
  339.           be used quite freely in a range of code words and reused to save
  340.           head space by replacing LABELs which have global names and cannot
  341.           be reused.
  342.           
  343.           The use of local labels is best demonstrated by an example taken
  344.           from the software floating point package SFLOAT.SEQ by Bob Smith,
  345.           shown in Figure 21.  Up to 32 local labels can be used to mark
  346.           addresses of assembly code.  They can be refered to before or
  347.           after their placements.  They can be referenced across code word
  348.           boundaries.  The command CLEAR-LABELS defines the boundary where
  349.           the local label referencing cannot cross.  Between two
  350.           consecutive CLEAR-LABELS, local labels can be freely placed and
  351.           referenced.
  352.           
  353.           This technique is especially useful where the one-entry-one-exit
  354.           dogma is very awkward when a piece of code has multiple entry
  355.           points and can be shared among many code word definitions.  It
  356.           allows us to construct structured spaghetti code, if there were
  357.           such thing.
  358.           
  359.  
  360. 7.   INLINE CODE
  361.  
  362.  
  363.           INLINE allows us to include machine code inside a high level
  364.           colon definition.  This is easily done in F-PC because it is
  365.           built on direct threaded code.  Every word is compiled as a code
  366.           address in the colon definition.  The code in the code field
  367.           pointed to by the code address is executed directly because it is
  368.           genuine 8086 machine code.  Whether the code belongs to a colon
  369.           definition or a code definition does not make any difference.
  370.           INLINE only has to compile the address pointing to the top of the
  371.           dictionary in the code segment.  The assembler can then be
  372.           invoked to compile machine code.  If the code is terminated by
  373.           NEXT or one of its derivatives, the next word compiled in the
  374.           colon definition will be executed after the assembly code is
  375.           done.  END-INLINE only has to clean up the assembly environment
  376.           and return the control back to the colon compiler.
  377.           
  378.           Here is an example on how to use INLINE and END-INLINE to add
  379.           assembly code in the middle of a colon definition:
  380.           
  381.                : TEST ( -- )
  382.                     5 0 DO
  383.                          I                   \ Get loop index
  384.                          INLINE         
  385.                               pop ax         \ pop I
  386.                               add ax, # 23   \ add 23
  387.                               1push          \ push sum
  388.                          END-INLINE
  389.                          .                   \ print results
  390.                     LOOP
  391.                     ;
  392.  
  393.